package com.example.farmer.Fragment.Home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.farmer.Activity.Main2Activity;
import com.example.farmer.Activity.SearchActivity;
import com.example.farmer.Adapter.Product.ViewPagerProductAdapter;
import com.example.farmer.Presenter.ProductPresenter;
import com.example.farmer.R;
import com.example.farmer.View.ProductView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class Buyer_HomeFragment extends Fragment implements ProductView {
    private ProductPresenter mPresenter;
    private ViewPager2 viewPager;
    private Button btnBuyerHome;
    private ImageView imageView;
    private TabLayout tabLayout;
    private ViewPagerProductAdapter viewPagerAdapter;

    public Buyer_HomeFragment() {
        // Required empty public constructor
    }

    public static Buyer_HomeFragment newInstance() {
        Buyer_HomeFragment fragment = new Buyer_HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_product, container, false);


        viewPager = view.findViewById(R.id.viewPager);
        tabLayout = view.findViewById(R.id.tabs);
        viewPagerAdapter = new ViewPagerProductAdapter(this);
        viewPager.setAdapter(viewPagerAdapter);
        mPresenter = new ProductPresenter(this);


        //2.点击搜索框（图片）跳转至搜索页面（activity）
        imageView = view.findViewById(R.id.imageView);        // 初始化ImageView
        imageView.setOnClickListener(new View.OnClickListener() {        // 设置点击事件监听器
            @Override
            public void onClick(View v) {                // 点击图片时，跳转到目标Activity
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });

        //3.买家首页跳转至卖家首页
        btnBuyerHome = view.findViewById(R.id.btnBuyerHome);        //点击按钮跳转到卖家首页（fragmen）
        btnBuyerHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Main2Activity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });

        // 初始化数据
        initData();

        // 设置TabLayout与ViewPager2关联
        tabLayout.addTab(tabLayout.newTab().setText("水果"));
        tabLayout.addTab(tabLayout.newTab().setText("蔬菜"));
        tabLayout.addTab(tabLayout.newTab().setText("种子"));
        // 添加其他Tab


        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 当Tab被选中时，获取其位置并通知Presenter
                int position = tab.getPosition();
                viewPager.setCurrentItem(position);
                mPresenter.onTabSelected(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 可以不实现
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 可以不实现
            }
        });

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }
        });
        return view;
    }

    private void initData() {
        // 模拟数据初始化，你需要根据实际情况来初始化数据
        viewPagerAdapter.setFruitList(getSampleList());
        viewPagerAdapter.setVegetableList(getSampleList());
        viewPagerAdapter.setSeedList(getSampleList());
    }

    private List<String> getSampleList() {
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 3; i++) {
            list.add("Item " + i);
        }
        return list;
    }


    // 实现ClassView接口方法
    @Override
    public void showFruit(List<String> fruitList) {
        // 更新水果UI
        viewPagerAdapter.setFruitList(fruitList);
        viewPager.setCurrentItem(0);
    }


    @Override
    public void showVegetable(List<String> vegetableList) {
        // 更新蔬菜UI
        viewPagerAdapter.setVegetableList(vegetableList);
        viewPager.setCurrentItem(1);
    }

    @Override
    public void showSeed(List<String> seedList) {
        // 更新种子UI
        viewPagerAdapter.setSeedList(seedList);
        viewPager.setCurrentItem(2);
    }

    @Override
    public void setPresenter(ProductPresenter productPresenter) {
        this.mPresenter = productPresenter;
    }

}